package com.apestech.framework.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.apestech.framework.file.callback.ReadBufferFileCallBackHandler;

/**
 * 读文件行操作，只支持全文本读，不支持光标移动式的读取
 * @author hupo
 *
 */
public class BufferFileReadSingle {
	
	private String fileName;
	private final File file;
	private int index=-1;
	private BufferedReader bufRead=null;
	private boolean closed=false;
	
	public BufferFileReadSingle(String fileName) throws FileNotFoundException{
		this.fileName=fileName;
		this.file=new File(this.fileName);
		this.index=-1;
		this.bufRead = new BufferedReader(new FileReader(file));
	}
	
	public String getFileName(){
		return this.fileName;
	}
	
	public boolean closed(){
		return this.closed;
	}
	
	public void read(ReadBufferFileCallBackHandler readBufferFileCallBackHandler) throws Exception{
		String text=this.bufRead.readLine();
		while(text!=null){
			this.index++;
			text=this.bufRead.readLine();
			readBufferFileCallBackHandler.read(text, this.index);
		}
	}
	
	public int getRowCount() throws Exception{
		if(this.index==-1){
			read(new ReadBufferFileCallBackHandler(){
				public boolean read(String text, int index) throws Exception {
					return false;
				}
				
			});
		}
		return this.index+1;
	}
		
	public void close() throws IOException{
		this.bufRead.close();
		this.bufRead=null;
		this.closed=true;
	}
	
	
	

}
